/**
 * Created by sunzhijun on 2019/02/19.
 */
import React, {Component} from 'react';
import ReferSearch from 'src/hrpub/common/components/referSearch/org';
import NCBackBtn from "src/hrpub/common/components/hr-back/index";
import './index.less';

class Header extends Component{
    constructor(props){
        super(props);
        this.state = {
            isCardFormPage : false, //是否是cardFormPage及显示分页器
            isCardPaginationVisible: false //是否显示卡片翻页区域
        }
        this.showHeadLeftFn = this.showHeadLeftFn.bind(this);
        this.handleBackBtn = this.handleBackBtn.bind(this);
        this.setCardPagination = this.setCardPagination.bind(this);
        this.setCardPaginationVisibleFn = this.setCardPaginationVisibleFn.bind(this);
        this.getCardFormId = this.getCardFormId.bind(this);
        this.setOrganizationValue = this.setOrganizationValue.bind(this);
    }

    //查询主组织 节点信息
    searchOrganization(val) {
        this.setState({
            orgValue: val
        },()=>{
            this.props.searchOrganizationChange && this.props.searchOrganizationChange(val);
        })
    }

    //给组织赋值
    setOrganizationValue(orgData){
        this.setState({
            orgValue: orgData
        })
    }

    //设置卡片翻页
    setCardPagination(pk_psndoc_sub){
        // console.log("pk_psndoc_sub",pk_psndoc_sub,this.props)
        let {cardPagination} = this.props;
        if(typeof(pk_psndoc_sub) == 'object' && pk_psndoc_sub.constructor == Array){
            pk_psndoc_sub = pk_psndoc_sub.toString();
        }
        cardPagination.setCardPaginationId({id: pk_psndoc_sub, status: 1});
    }
    //卡片翻页事件
    handlePageChange(props,pks){
        this.props.handleCardPagination && this.props.handleCardPagination(pks);
    }

    //操作按钮事件
    handleBotton(a,b){
        this.props.onBtnOperation && this.props.onBtnOperation(a,b);
    }

    //设置查询组织和返回按钮显示隐藏
    showHeadLeftFn(isShow,pk_psndoc_sub){
        this.setState({
            isCardFormPage: isShow,
            isCardPaginationVisible: isShow
        },()=>{
            if(!!pk_psndoc_sub){
                this.setCardPagination(pk_psndoc_sub);
            }
        })
    }

    //获取CardForm Id
    getCardFormId(id){
        this.setState({
            cardFormId: id
        })
    }


    //点击返回按钮事件
    handleBackBtn(callback){
        let { cardFormId } = this.state;
        let status = this.props.form.getFormStatus(cardFormId);
        if(status == 'edit'){
            this.props.cancelEditStatus && this.props.cancelEditStatus();
        }else{
            this.setState({
                isCardFormPage: false,
                isCardPaginationVisible: false
            })
            if(callback){
                callback()
            }else{
                this.props.handleBackClick && this.props.handleBackClick();
            }
            
        }
    }

    //设置卡片翻页显示隐藏
    setCardPaginationVisibleFn(isShow){
        this.setState({
            isCardPaginationVisible: isShow
        })
    }

    //获取header 区域高度
    getHeaderH(){
        return this.refs.getHeaderDom.offsetHeight;
    }

    render(){
        let {button, cardPagination} = this.props;
        let {isCardFormPage, isCardPaginationVisible, orgValue} = this.state;
        let {createButtonApp} = button;
        const {createCardPagination} = cardPagination;
        return (
            <div className="header nc-bill-header-area" ref="getHeaderDom">
                <div className={'leftDepartmentChoose'}>
                    <div className={isCardFormPage ? '' : 'hide'}>
                        <NCBackBtn onClick={ this.handleBackBtn }> </NCBackBtn>
                    </div>
                    <div className={isCardFormPage ? 'hide' : ''}>
                        <ReferSearch
                            orgVal={orgValue}
                            getOrgData={this.searchOrganization.bind(this)}
                        />  
                    </div>
                </div> 
                {/* 按钮区  btn-group */}
                <div className="btn-group">
                    {createButtonApp({
                        area: 'head',//按钮注册中的按钮区域
                        buttonLimit: 1,
                        onButtonClick: (props, btncode) => this.handleBotton(props, btncode),
                        popContainer: document.querySelector('.header-button-area')
                    })}

                    <div className="pagination-card" style={{display: isCardPaginationVisible ? '' : 'none'}}>
                        {createCardPagination({ 
                            handlePageInfoChange: this.handlePageChange.bind(this)
                        })} 
                    </div> 
                </div>
            </div>
        )
    }
}

export default Header;